﻿using System;
using System.Collections.Generic;
using System.Text;

using com.dz.ct.db;
using com.dz.ct.common;
using System.Data;

namespace com.dz.ct.business
{
    public class DBManager
    {
        BookDao m_book_dao = new BookDao();

      
        /// <summary>
        /// 检查小说是否存在
        /// </summary>
        /// <param name="i_book_name"></param>
        /// <param name="i_site_id"></param>
        /// <returns></returns>
        public BookInfo CheckBookEixsts(string i_book_name, int i_site_id)
        {
            if (string.IsNullOrEmpty(i_book_name))
                return null;

            i_book_name = i_book_name.Trim();

            try
            {
                return m_book_dao.CheckBookEixsts(i_book_name, i_site_id);
            }
           catch(Exception ex)
            {
                LogHelper.Error("检查小说是否存在失败" + ex.ToString());
                return null;
            }

        }

        /// <summary>
        /// 添加书的信息
        /// </summary>
        public BookInfo InsertBookInfo(BookInfo i_book)
        {
  
            if (string.IsNullOrEmpty(i_book.BookName))
                return null;
            i_book.BookName = i_book.BookName.Trim();
            i_book.BookAuthorName = i_book.BookAuthorName.Trim();
            i_book.BookCategoryName = i_book.BookCategoryName.Trim();
            i_book.BookSubCategroyName = i_book.BookSubCategroyName.Trim();
          
            try
            {
                return m_book_dao.InsertBookInfo(i_book);
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加书籍信息失败" + ex.ToString());
                return null;
            }
        }

            /// <summary>
        /// 添加章节信息
        /// </summary>
        /// <param name="i_volume_name"></param>
        /// <param name="i_chapter_name"></param>
        /// <param name="i_update_time"></param>
        /// <param name="i_is_vip"></param>
        /// <param name="i_words_count"></param>
        /// <param name="i_book_id"></param>
        /// <param name="i_site_id"></param>
        /// <returns></returns>
        public int InsertBookChapter(BookChapterInfo i_book_chapter_info)
        {
            if (string.IsNullOrEmpty(i_book_chapter_info.ChapterName))
                return Consts.Faild;
            i_book_chapter_info.ChapterName = i_book_chapter_info.ChapterName.Trim();
          
            try
            {
                if (AppConfig.ChapterType == 0)
                {
                    FileManager t_file_mgr = new FileManager();
                    if (t_file_mgr.WriteBookChapter(Consts.GetTableName(i_book_chapter_info.SiteId), i_book_chapter_info))
                        return Consts.Success;

                    return Consts.Faild;
                }
                return m_book_dao.InsertBookChapterInfo(i_book_chapter_info);
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加章节信息失败" + ex.ToString());
                return Consts.Faild;
            }
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="i_str_sql"></param>
        /// <returns></returns>
        public DataSet QueryData(string i_str_sql)
        {
            try
            {
                return m_book_dao.QueryData(i_str_sql);
            }
            catch (Exception ex)
            {
                LogHelper.Error("查询数据失败" + ex.ToString());
                return null;
            }
        }

        /// <summary>
        /// 更新章节的最后id
        /// </summary>
        /// <param name="i_chapter_id"></param>
        /// <param name="i_next_chapter_id"></param>
        /// <returns></returns>
        public int UpdateLastUpdateChapterId(string i_chapter_id, string i_next_chapter_id,string i_site_name)
        {
            string t_str_sql = string.Format("update tb{0} set nextChapterId='{0}' where chapterId='{1}'", i_site_name, i_next_chapter_id, i_chapter_id);

            try
            {
                //走文本流程
                if (AppConfig.ChapterType == 0)
                {
                    FileManager t_file_mgr = new FileManager();
                    if (t_file_mgr.WriteBookChapterSQL(i_site_name, t_str_sql))
                        return Consts.Success;

                    return Consts.Faild;
                }
                return m_book_dao.ExcuteSql(t_str_sql);
            }
            catch (Exception ex)
            {
                LogHelper.Error("更新章节nextchapterid失败" + ex.ToString());
                return Consts.Faild;
            }
        }

        /// <summary>
        /// 更新小说信息
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_last_update_chapter_id"></param>
        /// <param name="i_last_update_chapter_name"></param>
        /// <param name="i_last_update_time"></param>
        /// <param name="i_chapter_count"></param>
        /// <returns></returns>
        public int UpdateBookChapterInfo(string i_site, string i_book_id, string i_last_update_chapter_id, string i_last_update_chapter_name, DateTime i_last_update_time, int i_chapter_count)
        {
            string t_str_sql=string.Format("update tbbook set lastUpdateChapterId='{0}',lastUpdateChapterName='{1}',lastUpdateTime='{2}',bookChapterCount=bookChapterCount+{3} where bookId='{4}'",
                i_last_update_chapter_id,i_last_update_chapter_name,i_last_update_time,i_chapter_count,i_book_id);

            try
            {
                //走文本流程
                if (AppConfig.ChapterType == 0)
                {
                    FileManager t_file_mgr = new FileManager();
                    if (t_file_mgr.WriteBookChapterSQL(i_site, t_str_sql))
                        return Consts.Success;

                    return Consts.Faild;
                }

                return m_book_dao.ExcuteSql(t_str_sql);
            }
            catch (Exception ex)
            {
                LogHelper.Error("更新小说信息失败" + ex.ToString());
                return Consts.Faild;
            }
        }

    }
}
